package com.leiyuan.content.controller;

import com.leiyuan.content.service.CourseBaseInfoService;
import com.leiyuan.content.utils.SecurityUtil;
import com.leiyuan.dto.AddCourseDto;
import com.leiyuan.dto.CourseBaseInfoDto;
import com.leiyuan.dto.EditCourseDto;
import com.leiyuan.dto.QueryCourseParamsDto;
import com.leiyuan.exection.ElearningPlusException;
import com.leiyuan.exection.ValidationGroups;
import com.leiyuan.model.PageParams;
import com.leiyuan.model.PageResult;
import com.leiyuan.po.CourseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author leiyuan
 * @version 1.0
 * @description 课程信息编辑接口
 * @date 2023/4/14 21:35
 */
@Api(value = "课程信息编辑接口",tags = "课程信息编辑接口")
@RestController
@Slf4j
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAuthority('course_find_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody QueryCourseParamsDto queryCourseParams){

        //当前登录用户
        SecurityUtil.ElUser user = SecurityUtil.getUser();
        //机构id
        String companyId = user.getCompanyId();

        //调用service获取数据
        return courseBaseInfoService.queryCourseBaseList(Long.parseLong(companyId),pageParams,queryCourseParams);
    }

    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody
                                                  @Validated({ValidationGroups.Insert.class}) AddCourseDto addCourseDto){

        //取出当前用户身份
        SecurityUtil.ElUser user = SecurityUtil.getUser();
        log.info("当前用户身份：{}",user);
        Long companyId = Long.parseLong(user.getCompanyId());
        return courseBaseInfoService.createCourseBase(companyId,addCourseDto);
    }

    @ApiOperation("根据课程id查询课程基础信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        //取出当前用户身份
        SecurityUtil.ElUser user = SecurityUtil.getUser();
        log.info("当前用户身份：{}",user);
        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }

    @ApiOperation("修改课程基础信息")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto){
        //取出当前用户身份
        SecurityUtil.ElUser user = SecurityUtil.getUser();
        log.info("当前用户身份：{}",user);
        Long companyId = Long.parseLong(user.getCompanyId());
        log.info("编辑课程基本信息：{}",editCourseDto);
        return courseBaseInfoService.updateCourseBase(companyId,editCourseDto);
    }


}
